1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#!/usr/bin/env bats
load test_helper
setup() {
create_user
create_test_key "$TEST_KEY_NAME"
}
teardown() {
delete_user
delete_test_keys
}
check_authorized_keys_entry() {
# shellcheck disable=SC2034
local KEYFILE_NAME="$1"
local ENTRY_ID="$2"
run bash -c "sed -n 's/.*\(NAME=\\\\\"${ENTRY_ID}\\\\\"\).*/\1/p' /home/${TEST_USER}/.ssh/authorized_keys"
echo "entry: $(grep "$ENTRY_ID" "/home/${TEST_USER}/.ssh/authorized_keys")"
echo "output: $output"
echo "status: $status"
assert_output "NAME=\\\"$ENTRY_ID\\\""
}
check_custom_allowed_keys() {
local ALLOWED_KEYS="$1"
run bash -c "grep ${ALLOWED_KEYS} /home/${TEST_USER}/.ssh/authorized_keys"
echo "entry: $(cat "/home/${TEST_USER}/.ssh/authorized_keys")"
echo "output: $output"
echo "status: $status"
assert_success
}
@test "(core) sshcommand create" {
delete_user
run bash -c "sshcommand create $TEST_USER ls > /dev/null"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "test -f ~${TEST_USER}/.ssh/authorized_keys"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "grep -F ls ~${TEST_USER}/.sshcommand"
echo "output: $output"
echo "status: $status"
assert_success
}
@test "(core) sshcommand acl-add" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
create_test_key new_key
run bash -c "cat ${TEST_KEY_DIR}/new_key.pub | sshcommand acl-add $TEST_USER user2"
echo "output: $output"
echo "status: $status"
assert_success
check_authorized_keys_entry "$TEST_KEY_NAME" user1
check_authorized_keys_entry new_key user2
}
@test "(core) sshcommand acl-add (as argument)" {
run bash -c "sshcommand acl-add $TEST_USER user1 ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub"
echo "output: $output"
echo "status: $status"
assert_success
create_test_key new_key
run bash -c "sshcommand acl-add $TEST_USER user2 ${TEST_KEY_DIR}/new_key.pub"
echo "output: $output"
echo "status: $status"
assert_success
check_authorized_keys_entry "$TEST_KEY_NAME" user1
check_authorized_keys_entry new_key user2
}
@test "(core) sshcommand acl-add (custom allowed keys)" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | SSHCOMMAND_ALLOWED_KEYS=keys-user1 sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
create_test_key new_key
run bash -c "cat ${TEST_KEY_DIR}/new_key.pub | sshcommand acl-add $TEST_USER user2"
echo "output: $output"
echo "status: $status"
assert_success
check_authorized_keys_entry "$TEST_KEY_NAME" user1
check_authorized_keys_entry new_key user2
check_custom_allowed_keys keys-user1
check_custom_allowed_keys no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding
}
@test "(core) sshcommand acl-add (bad key failure)" {
run bash -c "echo test_key | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_failure
}
@test "(core) sshcommand acl-add (with identifier space)" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER 'broken user'"
echo "output: $output"
echo "status: $status"
assert_success
check_authorized_keys_entry "$TEST_KEY_NAME" 'broken user'
}
@test "(core) sshcommand acl-add (with authorized_keys with options)" {
run bash -c "sshcommand acl-add $TEST_USER user1 ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub"
echo "output: "$output
echo "status: "$status
assert_success
run bash -c "sshcommand acl-add $TEST_USER user2 /home/${TEST_USER}/.ssh/authorized_keys"
echo "output: "$output
echo "status: "$status
assert_failure
}
@test "(core) sshcommand acl-add (multiple keys)" {
create_test_key second_key
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub ${TEST_KEY_DIR}/second_key.pub | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_failure
}
@test "(core) sshcommand acl-add (duplicate key)" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_failure
check_authorized_keys_entry "$TEST_KEY_NAME" user1
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER user2"
echo "output: $output"
echo "status: $status"
assert_failure
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | SSHCOMMAND_CHECK_DUPLICATE_NAME=false sshcommand acl-add $TEST_USER user2"
echo "output: $output"
echo "status: $status"
assert_failure
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | SSHCOMMAND_CHECK_DUPLICATE_NAME=false SSHCOMMAND_CHECK_DUPLICATE_FINGERPRINT=false sshcommand acl-add $TEST_USER user2"
echo "output: $output"
echo "status: $status"
assert_success
}
@test "(core) sshcommand acl-remove" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "grep -F \"$(<"${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub")\" ~${TEST_USER}/.ssh/authorized_keys | grep user1"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "sshcommand acl-remove $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
run bash -c "grep -F \"$(<"${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub")\" ~${TEST_USER}/.ssh/authorized_keys | grep user1"
echo "output: $output"
echo "status: $status"
assert_failure
}
@test "(core) sshcommand list" {
run bash -c "cat ${TEST_KEY_DIR}/${TEST_KEY_NAME}.pub | SSHCOMMAND_ALLOWED_KEYS=keys-user1 sshcommand acl-add $TEST_USER user1"
echo "output: $output"
echo "status: $status"
assert_success
assert_equal \
"$(ssh-keygen -l -f "/home/${TEST_USER}/.ssh/authorized_keys" | awk '{print $2}') NAME=\"user1\" SSHCOMMAND_ALLOWED_KEYS=\"keys-user1\"" \
"$(sshcommand list "$TEST_USER")"
run bash -c "sshcommand acl-remove $TEST_USER user1 && sshcommand list"
echo "output: $output"
echo "status: $status"
assert_failure
cp tests/unit/fixtures/authorized_keys/input_variants "/home/${TEST_USER}/.ssh/authorized_keys"
run bash -c "sshcommand list $TEST_USER '' json"
echo "output: $output"
echo "status: $status"
assert_equal \
"$(head -n1 tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output)" \
"$(sshcommand list "$TEST_USER" "" json)"
}
@test "(core) sshcommand list (authorized_keys format variants)" {
cp tests/unit/fixtures/authorized_keys/input_variants "/home/${TEST_USER}/.ssh/authorized_keys"
run bash -c "sshcommand list $TEST_USER"
echo "output: $output"
echo "status: $status"
assert_equal \
"$(cat tests/unit/fixtures/authorized_keys/sshcommand_list_expected_output)" \
"$(sshcommand list "$TEST_USER")"
rm "/home/${TEST_USER}/.ssh/authorized_keys"
}
@test "(core) sshcommand list (json output)" {
cp tests/unit/fixtures/authorized_keys/input_variants "/home/${TEST_USER}/.ssh/authorized_keys"
run bash -c "sshcommand list $TEST_USER md5 json"
echo "output: $output"
echo "status: $status"
assert_equal \
"$(cat tests/unit/fixtures/authorized_keys/sshcommand_list_expected_json_output_md5_filtered)" \
"$(sshcommand list "$TEST_USER" "md5" json)"
rm "/home/${TEST_USER}/.ssh/authorized_keys"
}
@test "(core) sshcommand help" {
run bash -c "sshcommand help | wc -l"
echo "output: $output"
echo "status: $status"
[[ "$output" -ge 7 ]]
}
|