Skip to content

Commit 2459ae7

Browse files
author
François Chastanet
committed
Add SSH validation functions and update framework configuration
1 parent 7105f36 commit 2459ae7

File tree

8 files changed

+189
-1
lines changed

8 files changed

+189
-1
lines changed

.bash-compiler

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
FRAMEWORK_ROOT_DIR=${ROOT_DIR}
2+
TEMPLATES_ROOT_DIR=${HOME}/fchastanet/bash-compiler/examples/templates

src/Assert/symLinkValid.bats

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck source=src/batsHeaders.sh
4+
source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh"
5+
6+
# shellcheck source=/src//Assert/symLinkValid.sh
7+
source "${srcDir}/Assert/symLinkValid.sh"
8+
9+
teardown() {
10+
unstub_all
11+
rm -f "${BATS_TEST_TMPDIR}/myFile" "${BATS_TEST_TMPDIR}/link" || true
12+
}
13+
14+
function Assert::symLinkValid::notExists { #@test
15+
run Assert::symLinkValid "fileNotFound"
16+
assert_failure 1
17+
assert_lines_count 2
18+
assert_line --index 0 --partial "INFO - Check fileNotFound is a valid symlink"
19+
assert_line --index 1 --partial "ERROR - fileNotFound is not existing"
20+
}
21+
22+
function Assert::symLinkValid::notALink { #@test
23+
local file="${BATS_TEST_TMPDIR}/myFile"
24+
touch "${file}"
25+
run Assert::symLinkValid "${file}"
26+
assert_failure 2
27+
assert_lines_count 2
28+
assert_line --index 0 --partial "INFO - Check ${BATS_TEST_TMPDIR}/myFile is a valid symlink"
29+
assert_line --index 1 --partial "ERROR - ${BATS_TEST_TMPDIR}/myFile exists but is not a link"
30+
}
31+
32+
function Assert::symLinkValid::brokenLink { #@test
33+
local file="${BATS_TEST_TMPDIR}/myFile"
34+
touch "${file}"
35+
ln -sf "${file}" "${BATS_TEST_TMPDIR}/link"
36+
rm "${file}"
37+
run Assert::symLinkValid "${BATS_TEST_TMPDIR}/link"
38+
assert_failure 3
39+
assert_lines_count 2
40+
assert_line --index 0 --partial "INFO - Check ${BATS_TEST_TMPDIR}/link is a valid symlink"
41+
assert_line --index 1 --partial "ERROR - Broken link ${BATS_TEST_TMPDIR}/link"
42+
}
43+
44+
function Assert::symLinkValid::valid { #@test
45+
local file="${BATS_TEST_TMPDIR}/myFile"
46+
touch "${file}"
47+
ln -sf "${file}" "${BATS_TEST_TMPDIR}/link"
48+
run Assert::symLinkValid "${BATS_TEST_TMPDIR}/link"
49+
assert_success
50+
assert_lines_count 1
51+
assert_line --index 0 --partial "INFO - Check ${BATS_TEST_TMPDIR}/link is a valid symlink"
52+
}

src/Assert/symLinkValid.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# @description asserts that first argument is link to a file
4+
# that exists
5+
# @arg $1 link:String expected link
6+
# @env SUDO String allows to use custom sudo prefix command
7+
# @exitcode 1 if missing link
8+
# @exitcode 2 if not a link
9+
# @exitcode 3 if broken link (missing linked file)
10+
# @stderr diagnostics information is displayed
11+
Assert::symLinkValid() {
12+
local link="$1"
13+
Log::displayInfo "Check ${link} is a valid symlink"
14+
if ! ${SUDO:-} test -L "${link}" &>/dev/null; then
15+
if ! ${SUDO:-} test -e "${link}" &>/dev/null; then
16+
Log::displayError "${link} is not existing"
17+
exit 1
18+
fi
19+
Log::displayError "${link} exists but is not a link"
20+
return 2
21+
fi
22+
if ! ${SUDO:-} test -e "$(readlink "${link}")" &>/dev/null; then
23+
Log::displayError "Broken link ${link}"
24+
return 3
25+
fi
26+
}

src/Ssh/checkAccess.bats

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck source=src/batsHeaders.sh
4+
source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh"
5+
# shellcheck source=src/Ssh/checkAccess.sh
6+
source "${srcDir}/Ssh/checkAccess.sh"
7+
8+
setup() {
9+
export HOME="${BATS_TEST_TMPDIR}"
10+
mkdir -p "${BATS_TEST_TMPDIR}/.ssh"
11+
}
12+
13+
teardown() {
14+
unstub_all
15+
}
16+
17+
function Ssh::checkAccess::missingHost { #@test
18+
run Ssh::checkAccess
19+
20+
assert_failure 2
21+
assert_output --partial "ERROR - You must provide a host as first argument"
22+
}
23+
24+
function Ssh::checkAccess::sshFailure { #@test
25+
stub ssh '-q -o PubkeyAuthentication=yes -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ChallengeResponseAuthentication=no host exit : exit 1'
26+
run Ssh::checkAccess "host"
27+
28+
assert_failure 1
29+
assert_output --partial "INFO - Checking host can be accessed non interactively using ssh"
30+
}
31+
32+
function Ssh::checkAccess::hostOnlySshSuccess { #@test
33+
stub ssh '-q -o PubkeyAuthentication=yes -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ChallengeResponseAuthentication=no host exit : exit 0'
34+
run Ssh::checkAccess "host"
35+
36+
assert_success
37+
assert_output --partial "INFO - Checking host can be accessed non interactively using ssh"
38+
}
39+
40+
function Ssh::checkAccess::MultipleArgsSshSuccess { #@test
41+
stub ssh '-q -o PubkeyAuthentication=yes -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o ChallengeResponseAuthentication=no host arg1 arg2 exit : exit 0'
42+
run Ssh::checkAccess "host" arg1 arg2
43+
44+
assert_success
45+
assert_output --partial "INFO - Checking host can be accessed non interactively using ssh"
46+
}

src/Ssh/checkAccess.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# @description check if host can accessed using ssh private key
4+
# without requiring interactivity
5+
# @arg $1 host:String the host to join
6+
# @arg $@ args:String[] other parameters to pass to the ssh command
7+
# @exitcode 1 if ssh fails
8+
# @exitcode 2 if missing host argument
9+
# @require Ssh::requireSshCommand
10+
Ssh::checkAccess() {
11+
local host="$1"
12+
shift 1 || true
13+
if [[ -z "${host}" ]]; then
14+
Log::displayError "You must provide a host as first argument"
15+
exit 2
16+
fi
17+
Log::displayInfo "Checking ${host} can be accessed non interactively using ssh"
18+
ssh \
19+
-q \
20+
-o PubkeyAuthentication=yes \
21+
-o PasswordAuthentication=no \
22+
-o KbdInteractiveAuthentication=no \
23+
-o ChallengeResponseAuthentication=no \
24+
"${host}" "$@" exit
25+
}

src/Ssh/requireSshCommand.bats

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# shellcheck disable=SC2154
3+
# shellcheck disable=SC2034
4+
5+
# shellcheck source=src/batsHeaders.sh
6+
source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh"
7+
# shellcheck source=src/Ssh/requireSshCommand.sh
8+
source "${srcDir}/Ssh/requireSshCommand.sh"
9+
10+
function Ssh::requireSshCommand::failure { #@test
11+
function Assert::commandExists() {
12+
return 1
13+
}
14+
15+
run Ssh::requireSshCommand
16+
17+
assert_failure 1
18+
assert_output ""
19+
}
20+
21+
function Ssh::requireSshCommand::success { #@test
22+
function Assert::commandExists() {
23+
return 0
24+
}
25+
26+
run Ssh::requireSshCommand
27+
28+
assert_success
29+
assert_output ""
30+
}

src/Ssh/requireSshCommand.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
# @description ensure command ssh is available
4+
# @exitcode 1 if ssh command not available
5+
# @stderr diagnostics information is displayed
6+
Ssh::requireSshCommand() {
7+
Assert::commandExists ssh
8+
}

src/_binaries/commandDefinitions/frameworkConfig.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ compilerConfig: &defaultFrameworkConfig
1010
- ${FRAMEWORK_ROOT_DIR}/src
1111
binDir: ${FRAMEWORK_ROOT_DIR}/bin
1212
templateDirs:
13-
- ${COMPILER_ROOT_DIR}/examples/templates
13+
- ${TEMPLATES_ROOT_DIR}
1414
templateFile: binFile.gtpl
1515
relativeRootDirBasedOnTargetDir: ..
1616

0 commit comments

Comments
 (0)