Files
dokku/tests/unit/30_ssh_keys.bats
Flavio Poletti a2bacd8096 Patch GH #2446 "Adding a file with two ssh-key..."
This is a proposed change for addressing GH #2446 "Adding a file with
two ssh-keys can result in an error" (see
https://github.com/dokku/dokku/issues/2446).

The following checks are performed:

- validity of key contents when read from a file
- key contents only contains one single line/key
- authorized_keys file is ultimately valid

The last test is an extension over the previous implementation in that
all lines in the file are checked.

The commit also includes extensions to the relevant test file to
excercise the new checks. It also takes care to save and then restore
the authorized_keys file.
2016-10-16 16:32:16 +02:00

84 lines
2.4 KiB
Bash

#!/usr/bin/env bats
load test_helper
setup() {
global_setup
# create a temporary key and save in a variable
create_key
local KEY="$(cat /tmp/testkey.pub)"
destroy_key
# now create key that will be really used
create_key
# the temporary key is useful for adding in the file with two keys
# useful for a negative test
{ cat /tmp/testkey.pub ; echo "$KEY" ; } > /tmp/testkey-double.pub
# another negative test input
echo 'invalid!' > /tmp/testkey-invalid.pub
# save current authorized_keys to remove all changes afterwards
cp "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys" /tmp/testkey-authorized_keys
}
teardown() {
# restore authorized_keys to its contents before the tests
cp /tmp/testkey-authorized_keys "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"
# normal cleanup after here
destroy_key
global_teardown
}
@test "(ssh-keys) ssh-keys:add, ssh-keys:list, ssh-keys:remove" {
run /bin/bash -c "dokku ssh-keys:add name1 /tmp/testkey.pub"
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "cat /tmp/testkey.pub | dokku ssh-keys:add name2"
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "dokku ssh-keys:list | grep name1 && dokku ssh-keys:list | grep name2"
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "dokku ssh-keys:remove name1"
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "dokku ssh-keys:list | grep name1"
echo "output: "$output
echo "status: "$status
assert_failure
run /bin/bash -c "dokku ssh-keys:list | grep name2"
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "dokku ssh-keys:add name3 /tmp/testkey-double.pub"
echo "output: "$output
echo "status: "$status
assert_failure
run /bin/bash -c "dokku ssh-keys:add name4 /tmp/testkey-invalid.pub"
echo "output: "$output
echo "status: "$status
assert_failure
run /bin/bash -c "dokku ssh-keys:add name4 /tmp/testkey.pub"
echo "output: "$output
echo "status: "$status
assert_success
# leave this as the last test in the sequence! It introduces an error in authorized_keys
run /bin/bash -c 'echo invalid >> "${DOKKU_ROOT:-/home/dokku}/.ssh/authorized_keys"'
echo "output: "$output
echo "status: "$status
assert_success
run /bin/bash -c "dokku ssh-keys:add name5 /tmp/testkey.pub"
echo "output: "$output
echo "status: "$status
assert_failure
}