Skip to content

Commit 910729c

Browse files
committed
add executable-wrapper template but it unwraps recursively
1 parent 832ba4e commit 910729c

3 files changed

Lines changed: 106 additions & 55 deletions

File tree

src/artifacts-helper/install.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,11 @@ if [ "$WRAPPERTYPE" = "SHELL_FUNCTION" ]; then
148148
done
149149
elif [ "$WRAPPERTYPE" = "EXECUTABLE" ]; then
150150
echo "Installing executable scripts for wrapped commands."
151+
sudo mkdir -p "${EXECUTABLES_TARGET_DIR}"
151152

152153
for ALIAS in "${WRAPPED_BINS_ARR[@]}"; do
153154
CMD_LINE=(ln -s "/usr/local/bin/run-$ALIAS.sh" "${EXECUTABLES_TARGET_DIR}/$ALIAS")
154-
if [ "${INSTALL_WITH_SUDO}" = "true" ]; then
155-
sudo -u "${_REMOTE_USER}" "${CMD_LINE[@]}"
156-
else
157-
"${CMD_LINE[@]}"
158-
fi
155+
sudo "${CMD_LINE[@]}"
159156
done
160157
else
161158
echo "Invalid WRAPPERTYPE specified: $WRAPPERTYPE. Must be one of SHELL_FUNCTION or EXECUTABLE."
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
# Recursively unwrap itself from the path, eventually calling the target binary's
4+
# `run-<target>.sh` script, which will inject a feed authentication token.
5+
6+
TARGET_BIN_NAME=TARGET_BIN_NAME
7+
8+
log() {
9+
# Print to stderr to avoid interfering with scripts that capture stdout.
10+
# Prefix error messages so the user can quickly determine that it's from our
11+
# artifacts-helper wrapper, not an error originating from the wrapped binary.
12+
printf >&2 "[codespace-features/artifacts-helper] %s" "$1"
13+
}
14+
15+
log_line() {
16+
log "$1
17+
"
18+
}
19+
20+
call_wrapped_bin() {
21+
CURRENT_SCRIPT_PATH=$(realpath "${BASH_SOURCE[0]}")
22+
if [ -z "$CURRENT_SCRIPT_PATH" ]; then
23+
log_line "Error: Current script path could not be determined! This will result in infinite recursion. Bailing early."
24+
exit 1
25+
fi
26+
27+
# Find the next executable from the PATH order, which would be shadowed by this wrapper
28+
if [ -z "$ARTIFACTS_HELPER_TARGET_BIN_PATHS" ]; then
29+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(which -a "$TARGET_BIN_NAME")
30+
fi
31+
32+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(sed '/^[[:space:]]*$/d' <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
33+
ARTIFACTS_HELPER_TARGET_BIN_PATHS=$(grep --color=never -Fve "$CURRENT_SCRIPT_PATH" <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
34+
NEXT_SHADOWED_BIN=$(head -n 1 <<<"$ARTIFACTS_HELPER_TARGET_BIN_PATHS")
35+
if [ -z "$NEXT_SHADOWED_BIN" ]; then
36+
log_line "Error: The real $TARGET_BIN_NAME could not be found on PATH."
37+
log_line "which -a $TARGET_BIN_NAME:\n%s\n" "$(which -a $TARGET_BIN_NAME)"
38+
log_line "CC_SHADOWED_BINS:\n%s\n" "$ARTIFACTS_HELPER_TARGET_BIN_PATHS"
39+
log_line "CURRENT_SCRIPT_PATH: %s\n" "$CURRENT_SCRIPT_PATH"
40+
exit 1
41+
fi
42+
43+
# Recursively removing the wrapper script(s) from the shadowed bin paths will
44+
# allow us to account for circumstances where the wrapper script appears multiple
45+
# times on the PATH. We will eventually descend to the real target binary.
46+
export ARTIFACTS_HELPER_TARGET_BIN_PATHS
47+
48+
log_line "Running: %s\n" "$NEXT_SHADOWED_BIN"
49+
${NEXT_SHADOWED_BIN} "$@"
50+
return "$?"
51+
}
52+
53+
call_wrapped_bin "$@"
54+
exit $?

test/artifacts-helper/scenario_executable_wrapper.sh

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,66 @@ WRAPPER_INSTALL_PATH='/usr/local/share/codespace-features'
66
BINS_TO_CHECK=('yarn' 'npm' 'npx')
77

88
check_path_priority() {
9-
log "Checking PATH priority"
10-
11-
for BIN_NAME in "${BINS_TO_CHECK[@]}"; do
12-
if ! command -v "$BIN_NAME" &>/dev/null; then
13-
log "Error: $BIN_NAME not found in PATH"
14-
exit 1
15-
fi
16-
17-
# Check if the target binary is wrapped
18-
ACTUAL_BIN_PATH=$(command -v "$BIN_NAME")
19-
EXPECTED_BIN_PATH="$WRAPPER_INSTALL_PATH/$BIN_NAME"
20-
if ! grep -q "$EXPECTED_BIN_PATH" <<<"$ACTUAL_BIN_PATH"; then
21-
log "Error: $BIN_NAME is not wrapped. We expected $EXPECTED_BIN_PATH but the actual binary path is $ACTUAL_BIN_PATH."
22-
log "Please contact Clipchamp EngProd and let them know that the feed-auth-wrapper feature is not working as expected."
23-
exit 1
24-
fi
25-
26-
log "Success: $BIN_NAME is wrapped."
27-
done
9+
echo "Checking PATH priority"
10+
11+
for BIN_NAME in "${BINS_TO_CHECK[@]}"; do
12+
if ! command -v "$BIN_NAME" &>/dev/null; then
13+
echo "Error: $BIN_NAME not found in PATH"
14+
exit 1
15+
fi
16+
17+
# Check if the target binary is wrapped
18+
ACTUAL_BIN_PATH=$(command -v "$BIN_NAME")
19+
EXPECTED_BIN_PATH="$WRAPPER_INSTALL_PATH/$BIN_NAME"
20+
if ! grep -q "$EXPECTED_BIN_PATH" <<<"$ACTUAL_BIN_PATH"; then
21+
echo "Error: $BIN_NAME is not wrapped. We expected $EXPECTED_BIN_PATH but the actual binary path is $ACTUAL_BIN_PATH."
22+
echo "Please contact Clipchamp EngProd and let them know that the feed-auth-wrapper feature is not working as expected."
23+
exit 1
24+
fi
25+
26+
echo "Success: $BIN_NAME is wrapped."
27+
done
2828
}
2929

3030
check_bin_exec() {
31-
log "Checking if the wrapped binaries get executed"
32-
33-
WRAPPED_BINS_DIR=$(mktemp -d)
34-
BASH_BIN_DIR=$(dirname "$(command -v bash)")
35-
TEST_PATH="$WRAPPER_INSTALL_PATH:$WRAPPER_INSTALL_PATH:$WRAPPED_BINS_DIR:$BASH_BIN_DIR"
36-
37-
for BIN_NAME in "${BINS_TO_CHECK[@]}"; do
38-
log "Checking $BIN_NAME"
39-
log "Creating a temporary binary to be shadowed by the wrapper"
40-
WRAPPED_BIN="$WRAPPED_BINS_DIR/$BIN_NAME"
41-
expected_stdout="Hello from $BIN_NAME"
42-
cat <<EOF >"$WRAPPED_BIN"
31+
echo "Checking if the wrapped binaries get executed"
32+
33+
WRAPPED_BINS_DIR=$(mktemp -d)
34+
BASH_BIN_DIR=$(dirname "$(command -v bash)")
35+
TEST_PATH="$WRAPPER_INSTALL_PATH:$WRAPPER_INSTALL_PATH:$WRAPPED_BINS_DIR:$BASH_BIN_DIR"
36+
37+
for BIN_NAME in "${BINS_TO_CHECK[@]}"; do
38+
echo "Checking $BIN_NAME"
39+
echo "Creating a temporary binary to be shadowed by the wrapper"
40+
WRAPPED_BIN="$WRAPPED_BINS_DIR/$BIN_NAME"
41+
expected_stdout="Hello from $BIN_NAME"
42+
cat <<EOF >"$WRAPPED_BIN"
4343
#!/usr/bin/env bash
4444
if [ -z "\$ARTIFACTS_ACCESSTOKEN" ]; then
4545
echo "Error: ARTIFACTS_ACCESSTOKEN was not set! It should be set by the artifacts-helper wrapper."
4646
exit 1
4747
fi
4848
echo "$expected_stdout"
4949
EOF
50-
chmod +x "$WRAPPED_BIN"
51-
52-
log "Executing $BIN_NAME to check if it wraps the temporary binary"
53-
actual_stdout=$(PATH="$TEST_PATH" "$BIN_NAME")
54-
actual_stderr=$(PATH="$TEST_PATH" "$BIN_NAME" 2>&1 > /dev/null)
55-
56-
log "Checking the output of the wrapper"
57-
log "stdout: $actual_stdout"
58-
log "stderr: $actual_stderr"
59-
if [ "$actual_stdout" = "$expected_stdout" ]; then
60-
log "Success: wrapper for $BIN_NAME executed correctly."
61-
else
62-
log "Error: wrapper for $BIN_NAME did not execute correctly. Expected '$expected_stdout' but got '$actual_stdout'."
63-
exit 1
64-
fi
65-
done
66-
67-
rm -r "$WRAPPED_BINS_DIR"
68-
log "Success: Wrapped binaries are executed correctly."
50+
chmod +x "$WRAPPED_BIN"
51+
52+
echo "Executing $BIN_NAME to check if it wraps the temporary binary"
53+
actual_stdout=$(PATH="$TEST_PATH" "$BIN_NAME")
54+
actual_stderr=$(PATH="$TEST_PATH" "$BIN_NAME" 2>&1 >/dev/null)
55+
56+
echo "Checking the output of the wrapper"
57+
echo "stdout: $actual_stdout"
58+
echo "stderr: $actual_stderr"
59+
if [ "$actual_stdout" = "$expected_stdout" ]; then
60+
echo "Success: wrapper for $BIN_NAME executed correctly."
61+
else
62+
echo "Error: wrapper for $BIN_NAME did not execute correctly. Expected '$expected_stdout' but got '$actual_stdout'."
63+
exit 1
64+
fi
65+
done
66+
67+
rm -r "$WRAPPED_BINS_DIR"
68+
echo "Success: Wrapped binaries are executed correctly."
6969
}
7070

7171
main() {

0 commit comments

Comments
 (0)