Skip to content

Commit 797374f

Browse files
authored
Adding more integration tests and refactoring (#27)
Signed-off-by: Daniel Liszka <[email protected]>
1 parent 3a33712 commit 797374f

File tree

10 files changed

+228
-55
lines changed

10 files changed

+228
-55
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828

2929
- name: ${{ matrix.key }}
3030
run: make -C tools ${{ matrix.key }}
31+
env:
32+
CHAINLOOP_TOKEN: ${{ secrets.CHAINLOOP_TOKEN }}
3133

3234
test_build:
3335
name: Test if script generation works

tools/Makefile

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
VERSION=$(shell git describe --tags --always)
2+
# CHAINLOOP_TOKEN is required for testing and should be places in .env file
3+
DOCKER_TEST_CMD=docker run --rm -it -w /code -v "${PWD}:/code" --env-file .env $(DOCKER_IMG) ./test/run.sh
24

35
.PHONY: build
46
# build
@@ -10,19 +12,14 @@ build:
1012
test:
1113
./test/bats/bin/bats --print-output-on-failure test
1214

13-
docker_test_on_ubuntu:
14-
docker run --rm -it -w /code -v "${PWD}:/code" node ./test/bats/bin/bats --print-output-on-failure test
15-
16-
docker_test_on_alpine:
17-
docker run --rm -it -w /code -v "${PWD}:/code" chainguard/bash "./test/bats/bin/bats --print-output-on-failure test"
18-
19-
docker_test_on_ubi:
20-
docker run --rm -it -w /code -v "${PWD}:/code" redhat/ubi9 ./test/bats/bin/bats --print-output-on-failure test
21-
22-
docker_test: docker_test_on_ubuntu
23-
24-
docker_test_all: docker_test_on_ubuntu docker_test_on_alpine docker_test_on_ubi
25-
15+
docker_test_on_ubuntu: DOCKER_IMG=node
16+
docker_test_on_alpine: DOCKER_IMG=chainguard/bash
17+
docker_test_on_ubi: DOCKER_IMG=redhat/ubi9
18+
docker_test_on_ubuntu docker_test_on_alpine docker_test_on_ubi:
19+
@echo "\n\n### $@: Testing on $(DOCKER_IMG)"
20+
$(DOCKER_TEST_CMD)
21+
docker_test: build docker_test_on_ubuntu
22+
docker_test_all: build docker_test_on_ubuntu docker_test_on_alpine docker_test_on_ubi
2623
docker_test_shell:
2724
docker run --rm -it -w /code -v "${PWD}:/code" node bash
2825

tools/c8l

Lines changed: 132 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ version_command() {
1313

1414
c8l_usage() {
1515
if [[ -n $long_usage ]]; then
16-
printf "c8l - [EXPERIMENTAL] Chainloop (c8) Labs CLI\n"
16+
printf "c8l - [EXPERIMENTAL] (c8l) Chainloop Labs CLI\n"
1717
echo
1818

1919
else
20-
printf "c8l - [EXPERIMENTAL] Chainloop (c8) Labs CLI\n"
20+
printf "c8l - [EXPERIMENTAL] (c8l) Chainloop Labs CLI\n"
2121
echo
2222

2323
fi
@@ -32,6 +32,7 @@ c8l_usage() {
3232
printf " %s Show help about a command\n" "help "
3333
printf " %s [i] Inspect.\n" "inspect"
3434
printf " %s Show the content of c8l script ready for sourcing.\n" "source "
35+
printf " %s Run a command in the c8l environment.\n" "cmd "
3536
printf " %s Chainloop CLI UX improved\n" "cli "
3637
echo
3738

@@ -132,6 +133,41 @@ c8l_source_usage() {
132133
fi
133134
}
134135

136+
c8l_cmd_usage() {
137+
if [[ -n $long_usage ]]; then
138+
printf "c8l cmd - Run a command in the c8l environment.\n"
139+
echo
140+
141+
else
142+
printf "c8l cmd - Run a command in the c8l environment.\n"
143+
echo
144+
145+
fi
146+
147+
printf "Alias: r\n"
148+
echo
149+
150+
printf "%s\n" "Usage:"
151+
printf " c8l cmd COMMAND\n"
152+
printf " c8l cmd --help | -h\n"
153+
echo
154+
155+
if [[ -n $long_usage ]]; then
156+
printf "%s\n" "Options:"
157+
158+
printf " %s\n" "--help, -h"
159+
printf " Show this help\n"
160+
echo
161+
162+
printf "%s\n" "Arguments:"
163+
164+
printf " %s\n" "COMMAND"
165+
printf " Command to run in the c8l environment.\n"
166+
echo
167+
168+
fi
169+
}
170+
135171
c8l_cli_usage() {
136172
if [[ -n $long_usage ]]; then
137173
printf "c8l cli - Chainloop CLI UX improved\n"
@@ -560,7 +596,7 @@ normalize_input() {
560596
done
561597
}
562598

563-
export CHAINLOOP_BIN_PATH="${CHAINLOOP_BIN_PATH:-/usr/local/bin/chainloop}"
599+
export CHAINLOOP_BIN_PATH="${CHAINLOOP_BIN_PATH:-/usr/local/bin/chainloop_bin}"
564600

565601
is_chainloop_in_path() {
566602
if command -v chainloop &>/dev/null; then
@@ -605,11 +641,11 @@ prepare_tmp_file() {
605641
file_name=$1
606642
mkdir -p "${tmp_dir}"
607643
t="${tmp_dir}/${file_name}"
608-
if [ -f $t ]; then
644+
if [ -f "$t" ]; then
609645
echo "Temporary file file $t already exists"
610646
return 1
611647
fi
612-
echo $t
648+
echo "$t"
613649
}
614650

615651
# chainloop_bin_cache_in_dir - it takes a path and copy there the CHAINLOOP_BIN_PATH
@@ -632,6 +668,7 @@ chainloop_recreate_env_from_file() {
632668
file=$(basename $path)
633669
if [[ $file =~ ^\.env_.*$ ]]; then
634670
export $(echo $file | sed 's/\.env_//')=$(cat $path)
671+
echo export $(echo $file | sed 's/\.env_//')=$(cat $path)
635672
else
636673
log_error "File $file is not in the format .env_NAME"
637674
return 1
@@ -761,6 +798,7 @@ generic_install() {
761798
mkdir -p $CHAINLOOP_BIN_PATH
762799
log "Installing $file"
763800
curl -sfL $url -o $file_path
801+
764802
if [ $? -ne 0 ]; then
765803
log_error "$file installation failed"
766804
return 1
@@ -820,25 +858,32 @@ chainloop_attestation_push() {
820858
export COSIGN_PASSWORD="$CHAINLOOP_SIGNING_PASSWORD"
821859
cosign generate-key-pair
822860
fi
823-
if [ -z "${CHAINLOOP_SIGNING_KEY_PATH+x}" ]; then
861+
if [ -n "${CHAINLOOP_SIGNING_KEY}" ]; then
824862
log " with CHAINLOOP_SIGNING_KEY"
825863
tmp_key="${CHAINLOOP_TMP_DIR}/key"
826864
mkdir -p "${CHAINLOOP_TMP_DIR}"
827-
echo "${CHAINLOOP_SIGNING_KEY}" >$tmp_key
828-
else
865+
echo "${CHAINLOOP_SIGNING_KEY}" > "$tmp_key"
866+
fi
867+
if [ -n "${CHAINLOOP_SIGNING_KEY_PATH}" ]; then
829868
log " with CHAINLOOP_SIGNING_KEY_PATH"
830869
tmp_key="${CHAINLOOP_SIGNING_KEY_PATH}"
831870
fi
871+
872+
tmp_key_value=""
873+
if [ -n "$tmp_key" ]; then
874+
tmp_key_value="--key $tmp_key"
875+
fi
876+
832877
# chainloop attestation push --key env://CHAINLOOP_SIGNING_KEY
833-
if chainloop attestation push --key $tmp_key --remote-state --attestation-id "${CHAINLOOP_ATTESTATION_ID}" &>c8-push.txt; then
878+
if chainloop attestation push "$tmp_key_value" --output json --remote-state --attestation-id "${CHAINLOOP_ATTESTATION_ID}" > c8-push.txt; then
834879
log "Attestation Process Completed Successfully"
835880
cat c8-push.txt
836-
rm $tmp_key
881+
rm -f "$tmp_key"
837882
else
838883
exit_code=$?
839884
log_error "Attestation Process Failed"
840885
cat c8-push.txt
841-
rm $tmp_key
886+
rm -f "$tmp_key"
842887
return $exit_code
843888
fi
844889
}
@@ -849,20 +894,24 @@ chainloop_summary() {
849894
log $tmpfile
850895
return 1
851896
fi
852-
echo -e "## Great job!\n\nYou are making SecOps and Compliance teams really happy. Keep up the good work!\n" >>$tmpfile
897+
echo -e "## Great job!\n\nYou are making SecOps and Compliance teams really happy. Keep up the good work!\n" >> $tmpfile
853898

854899
digest=""
855900
if [ -f c8-push.txt ]; then
856-
digest=$(cat c8-push.txt | grep " Digest: " | awk -F'sha256:' '{print $2}')
857-
echo "**[Chainloop Trust Report]( https://app.chainloop.dev/attestation/sha256:${digest} )**" >>$tmpfile
858-
echo "\`\`\`" >>$tmpfile
901+
digest=$(cat c8-push.txt | jq -r '.digest')
902+
if [ $? -ne 0 ]; then
903+
log_error "Failed to get digest from c8-push.txt"
904+
return 1
905+
fi
906+
echo "**[Chainloop Trust Report]( https://app.chainloop.dev/attestation/${digest} )**" >> "$tmpfile"
859907
fi
860-
if [ -f c8-status.txt ]; then
861-
cat c8-status.txt >>$tmpfile
862-
echo "\`\`\`" >>$tmpfile
908+
if [ -f c8-status.txt ] ; then
909+
echo "\`\`\`" >> "$tmpfile"
910+
cat c8-status.txt >> "$tmpfile"
911+
echo "\`\`\`" >> "$tmpfile"
863912
fi
864-
cat $tmpfile
865-
rm $tmpfile
913+
cat "$tmpfile"
914+
rm "$tmpfile"
866915
}
867916

868917
chainloop_summary_on_failure() {
@@ -1076,6 +1125,11 @@ c8l_source_command() {
10761125

10771126
}
10781127

1128+
c8l_cmd_command() {
1129+
eval "${args['command']}"
1130+
1131+
}
1132+
10791133
c8l_cli_install_tools_command() {
10801134
validate_env
10811135
install_chainloop_tools
@@ -1272,6 +1326,13 @@ parse_requirements() {
12721326
shift $#
12731327
;;
12741328

1329+
cmd | r)
1330+
action="cmd"
1331+
shift
1332+
c8l_cmd_parse_requirements "$@"
1333+
shift $#
1334+
;;
1335+
12751336
cli | c)
12761337
action="cli"
12771338
shift
@@ -1432,6 +1493,56 @@ c8l_source_parse_requirements() {
14321493

14331494
}
14341495

1496+
c8l_cmd_parse_requirements() {
1497+
1498+
while [[ $# -gt 0 ]]; do
1499+
case "${1:-}" in
1500+
--help | -h)
1501+
long_usage=yes
1502+
c8l_cmd_usage
1503+
exit
1504+
;;
1505+
1506+
*)
1507+
break
1508+
;;
1509+
1510+
esac
1511+
done
1512+
1513+
action="cmd"
1514+
1515+
while [[ $# -gt 0 ]]; do
1516+
key="$1"
1517+
case "$key" in
1518+
1519+
-?*)
1520+
printf "invalid option: %s\n" "$key" >&2
1521+
exit 1
1522+
;;
1523+
1524+
*)
1525+
1526+
if [[ -z ${args['command']+x} ]]; then
1527+
args['command']=$1
1528+
shift
1529+
else
1530+
printf "invalid argument: %s\n" "$key" >&2
1531+
exit 1
1532+
fi
1533+
1534+
;;
1535+
1536+
esac
1537+
done
1538+
1539+
if [[ -z ${args['command']+x} ]]; then
1540+
printf "missing required argument: COMMAND\nusage: c8l cmd COMMAND\n" >&2
1541+
exit 1
1542+
fi
1543+
1544+
}
1545+
14351546
c8l_cli_parse_requirements() {
14361547

14371548
while [[ $# -gt 0 ]]; do
@@ -2044,6 +2155,7 @@ run() {
20442155
"help") c8l_help_command ;;
20452156
"inspect") c8l_inspect_command ;;
20462157
"source") c8l_source_command ;;
2158+
"cmd") c8l_cmd_command ;;
20472159
"cli") c8l_cli_command ;;
20482160
"cli install-tools") c8l_cli_install_tools_command ;;
20492161
"cli attestation-add-from-yaml") c8l_cli_attestation_add_from_yaml_command ;;

tools/src/bashly.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: c8l
2-
help: "[EXPERIMENTAL] Chainloop (c8) Labs CLI"
2+
help: "[EXPERIMENTAL] (c8l) Chainloop Labs CLI"
33
version: 0.3.0
44

55
commands:
@@ -14,6 +14,14 @@ commands:
1414
- name: source
1515
help: "Show the content of c8l script ready for sourcing."
1616

17+
- name: cmd
18+
help: "Run a command in the c8l environment."
19+
alias: r
20+
args:
21+
- name: command
22+
required: true
23+
help: Command to run in the c8l environment.
24+
1725
- name: cli
1826
alias: c
1927
help: Chainloop CLI UX improved

tools/src/cmd_command.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eval "${args['command']}"

tools/src/lib/base.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export CHAINLOOP_BIN_PATH="${CHAINLOOP_BIN_PATH:-/usr/local/bin/chainloop}"
1+
export CHAINLOOP_BIN_PATH="${CHAINLOOP_BIN_PATH:-/usr/local/bin/chainloop_bin}"
22

33
is_chainloop_in_path() {
44
if command -v chainloop &>/dev/null; then
@@ -43,9 +43,9 @@ prepare_tmp_file() {
4343
file_name=$1
4444
mkdir -p "${tmp_dir}"
4545
t="${tmp_dir}/${file_name}"
46-
if [ -f $t ]; then
46+
if [ -f "$t" ]; then
4747
echo "Temporary file file $t already exists"
4848
return 1
4949
fi
50-
echo $t
50+
echo "$t"
5151
}

tools/src/lib/cache.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ chainloop_recreate_env_from_file() {
1919
file=$(basename $path)
2020
if [[ $file =~ ^\.env_.*$ ]]; then
2121
export $(echo $file | sed 's/\.env_//')=$(cat $path)
22+
echo export $(echo $file | sed 's/\.env_//')=$(cat $path)
2223
else
2324
log_error "File $file is not in the format .env_NAME"
2425
return 1

0 commit comments

Comments
 (0)