From 30d8931ec545ac992b2eeb9755e865b2aae70a35 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sun, 25 Jan 2015 20:41:39 +0100 Subject: [PATCH 1/3] Issue #56: Fix arguments --- bin/juju | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bin/juju b/bin/juju index 903ca8f..8573445 100755 --- a/bin/juju +++ b/bin/juju @@ -122,7 +122,7 @@ function parse_arguments(){ OPT_DELETE=false OPT_HELP=false OPT_VERSION=false - for opt in $@ + for opt in "$@" do case "$1" in -i|--setup-from-file) OPT_SETUP_FROM_FILE=true ; shift ; IMAGE_FILE=$1 ; shift ;; @@ -140,8 +140,9 @@ function parse_arguments(){ done ARGS=() - for arg do - ARGS+=($arg) + for arg in "$@" + do + ARGS+=("$arg") done } @@ -169,11 +170,11 @@ function execute_operation(){ fi if $OPT_FAKEROOT; then - run_juju_as_fakeroot "${PROOT_ARGS}" ${ARGS[@]} + run_juju_as_fakeroot "${PROOT_ARGS}" "${ARGS[@]}" elif $OPT_ROOT; then - run_juju_as_root ${ARGS[@]} + run_juju_as_root "${ARGS[@]}" else - run_juju_as_user "${PROOT_ARGS}" ${ARGS[@]} + run_juju_as_user "${PROOT_ARGS}" "${ARGS[@]}" fi } From 30f630973d1f57af529217314316df9b4355c8b4 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Tue, 27 Jan 2015 00:44:41 +0100 Subject: [PATCH 2/3] Issue #56: Allow quotes on command passed to CLI --- lib/core.sh | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/core.sh b/lib/core.sh index cecde49..30af5e1 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -71,7 +71,7 @@ fi PROOT_COMPAT="${JUJU_HOME}/opt/proot/proot-${ARCH}" PROOT_LINK=http://static.proot.me/proot-${ARCH} -SH="/bin/sh --login" +SH=("/bin/sh" "--login") CHROOT=${JUJU_HOME}/usr/bin/arch-chroot TRUE=${JUJU_HOME}/usr/bin/true ID="${JUJU_HOME}/usr/bin/id -u" @@ -149,7 +149,7 @@ function setup_from_file_juju(){ function _define_chroot_args(){ - local comm=${SH} + local comm=${SH[@]} [ "$1" != "" ] && comm="$@" echo $comm } @@ -162,13 +162,14 @@ function run_juju_as_root(){ function _run_proot(){ local proot_args="$1" + shift if ${PROOT_COMPAT} $proot_args ${TRUE} &> /dev/null then - JUJU_ENV=1 ${PROOT_COMPAT} ${@} + JUJU_ENV=1 ${PROOT_COMPAT} $proot_args "${@}" elif PROOT_NO_SECCOMP=1 ${PROOT_COMPAT} $proot_args ${TRUE} &> /dev/null then warn "Proot error: Trying to execute proot with PROOT_NO_SECCOMP=1..." - JUJU_ENV=1 PROOT_NO_SECCOMP=1 ${PROOT_COMPAT} ${@} + JUJU_ENV=1 PROOT_NO_SECCOMP=1 ${PROOT_COMPAT} $proot_args "${@}" else die "Error: Check if the juju arguments are correct or use the option juju -p \"-k 3.10\"" fi @@ -176,26 +177,33 @@ function _run_proot(){ function _run_juju_with_proot(){ - [ "$(_run_proot "" ${ID} 2> /dev/null )" == "0" ] && \ - die "You cannot access with root privileges. Use --root option instead." + local proot_args="$1" + shift - _run_proot "${@}" + if [ "$1" != "" ] + then + _run_proot "${proot_args}" "${@}" + else + _run_proot "${proot_args}" "${SH[@]}" + fi } function run_juju_as_fakeroot(){ local proot_args="$1" shift - local comm=$(_define_chroot_args "$@") - _run_juju_with_proot "-S ${JUJU_HOME} ${proot_args}" ${comm} + [ "$(_run_proot "-R ${JUJU_HOME} $proot_args" ${ID} 2> /dev/null )" == "0" ] && \ + die "You cannot access with root privileges. Use --root option instead." + _run_juju_with_proot "-S ${JUJU_HOME} $proot_args" "${@}" } function run_juju_as_user(){ local proot_args="$1" shift - local comm=$(_define_chroot_args "$@") - _run_juju_with_proot "-R ${JUJU_HOME} $proot_args" ${comm} + [ "$(_run_proot "-R ${JUJU_HOME} $proot_args" ${ID} 2> /dev/null )" == "0" ] && \ + die "You cannot access with root privileges. Use --root option instead." + _run_juju_with_proot "-R ${JUJU_HOME} $proot_args" "${@}" } From e82cc672f64239e59c28df749863fdfae452f0bd Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Tue, 27 Jan 2015 01:03:51 +0100 Subject: [PATCH 3/3] Issue #56: Fix tests --- tests/test_core.sh | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/test_core.sh b/tests/test_core.sh index 8696b95..6421d09 100755 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -114,18 +114,26 @@ function test_run_juju_as_root(){ function test_run_juju_as_user(){ install_mini_juju - local output=$(run_juju_as_user "-k 3.10" "/usr/bin/mkdir -v /newdir2" | awk -F: '{print $1}') + local output=$(run_juju_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2" | awk -F: '{print $1}') is_equal "$output" "/usr/bin/mkdir" || return 1 [ -e $JUJU_HOME/newdir2 ] is_equal $? 0 || return 1 - SH="/usr/bin/mkdir -v /newdir" + SH=("/usr/bin/mkdir" "-v" "/newdir") local output=$(run_juju_as_user "-k 3.10" | awk -F: '{print $1}') is_equal "$output" "/usr/bin/mkdir" || return 1 [ -e $JUJU_HOME/newdir ] is_equal $? 0 || return 1 } +function test_run_juju_with_quotes(){ + install_mini_juju + local output=$(run_juju_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2" | awk -F: '{print $1}') + is_equal "$output" "/usr/bin/mkdir" || return 1 + [ -e $JUJU_HOME/newdir2 ] + is_equal $? 0 || return 1 +} + function test_run_juju_as_user_proot_args(){ install_mini_juju run_juju_as_user "--help" "" &> /dev/null @@ -133,7 +141,7 @@ function test_run_juju_as_user_proot_args(){ mkdir $JUJU_TEMPDIR/newdir touch $JUJU_TEMPDIR/newdir/newfile - run_juju_as_user "-b $JUJU_TEMPDIR/newdir:/newdir -k 3.10" "ls -l /newdir/newfile" &> /dev/null + run_juju_as_user "-b $JUJU_TEMPDIR/newdir:/newdir -k 3.10" "ls" "-l" "/newdir/newfile" &> /dev/null is_equal $? 0 || return 1 export -f _run_juju_with_proot @@ -141,7 +149,7 @@ function test_run_juju_as_user_proot_args(){ export -f warn export -f die export PROOT_COMPAT - ID="/bin/echo 1" bash -ic "_run_juju_with_proot --helps" &> /dev/null + bash -ic "_run_juju_with_proot --helps" &> /dev/null is_equal $? 1 || return 1 export -n _run_juju_with_proot export -n _run_proot @@ -159,7 +167,7 @@ function test_run_juju_with_proot_compat(){ export -f _run_proot export -f warn export -f die - PROOT_COMPAT="/bin/false" ID="/bin/echo 1" bash -ic "_run_juju_with_proot --helps" &> /dev/null + PROOT_COMPAT="/bin/false" bash -ic "_run_juju_with_proot --helps" &> /dev/null is_equal $? 1 || return 1 export -n _run_juju_with_proot export -n _run_proot @@ -169,15 +177,23 @@ function test_run_juju_with_proot_compat(){ function test_run_juju_with_proot_as_root(){ install_mini_juju - export -f _run_juju_with_proot + export -f _run_proot + export -f run_juju_as_user + export -f run_juju_as_fakeroot export TRUE export PROOT_COMPAT - ID="/bin/echo 0" bash -ic "_run_juju_with_proot" &> /dev/null + ID="/bin/echo 0" bash -ic "run_juju_as_user" &> /dev/null + is_equal $? 1 || return 1 + ID="/bin/echo 0" bash -ic "run_juju_as_fakeroot" &> /dev/null is_equal $? 1 || return 1 export -n TRUE export -n PROOT_COMPAT - export -n _run_juju_with_proot - unset _run_juju_with_proot + export -n _run_proot + export -n run_juju_as_user + export -n run_juju_as_fakeroot + unset _run_proot + unset run_juju_as_user + unset run_juju_as_fakeroot } function test_run_proot_seccomp(){