-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc8l
executable file
·2029 lines (1581 loc) · 41.8 KB
/
c8l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# This script was generated by bashly 1.2.0 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi
version_command() {
echo "$version"
}
c8l_usage() {
printf "c8l - [EXPERIMENTAL] (c8l) Chainloop Labs CLI\n\n"
printf "%s\n" "Usage:"
printf " c8l COMMAND\n"
printf " c8l [COMMAND] --help | -h\n"
printf " c8l --version | -v\n"
echo
printf "%s\n" "Commands:"
printf " %s Show help about a command\n" "help "
printf " %s [i] Inspect.\n" "inspect"
printf " %s Show the content of c8l script ready for sourcing.\n" "source "
printf " %s Run a command in the c8l environment.\n" "cmd "
printf " %s Chainloop CLI UX improved\n" "cli "
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf " %s\n" "--version, -v"
printf " Show version number\n"
echo
fi
}
c8l_help_usage() {
printf "c8l help - Show help about a command\n\n"
printf "Alias: h\n"
echo
printf "%s\n" "Usage:"
printf " c8l help\n"
printf " c8l help --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_inspect_usage() {
printf "c8l inspect - [i] Inspect.\n\n"
printf "Alias: i\n"
echo
printf "%s\n" "Usage:"
printf " c8l inspect\n"
printf " c8l inspect --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_source_usage() {
printf "c8l source - Show the content of c8l script ready for sourcing.\n\n"
printf "%s\n" "Usage:"
printf " c8l source\n"
printf " c8l source --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cmd_usage() {
printf "c8l cmd - Run a command in the c8l environment.\n\n"
printf "Alias: r\n"
echo
printf "%s\n" "Usage:"
printf " c8l cmd COMMAND\n"
printf " c8l cmd --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf "%s\n" "Arguments:"
printf " %s\n" "COMMAND"
printf " Command to run in the c8l environment.\n"
echo
fi
}
c8l_cli_usage() {
printf "c8l cli - Chainloop CLI UX improved\n\n"
printf "Alias: c\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli COMMAND\n"
printf " c8l cli [COMMAND] --help | -h\n"
echo
printf "%s\n" "Commands:"
printf " %s [it] Install Chainloop CLI and all required tools\n" "install-tools "
printf " %s [aafy] Add to the current atestation based on the yaml file.\n" "attestation-add-from-yaml"
printf " %s [as] Get the status of the current attestation.\n" "attestation-status "
printf " %s [ap] Push the current attestation to the Chainloop server.\n" "attestation-push "
printf " %s [ggs] Generate a summary of the attestation for GitHub Action.\n" "generate-github-summary "
printf " %s [ga] Get all attestations for artifact\n" "get-attestations "
printf " %s [g] Get artifact from Chainloop\n" "get "
printf " %s [wg] Get workflow from Chainloop.\n" "workflow-get "
printf " %s [wl] List workflows from Chainloop.\n" "workflow-list "
printf " %s [wrg] Get workflow run from Chainloop.\n" "workflow-run-get "
printf " %s [wrl] List workflow runs from Chainloop.\n" "workflow-run-list "
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_install_tools_usage() {
printf "c8l cli install-tools - [it] Install Chainloop CLI and all required tools\n\n"
printf "Alias: it\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli install-tools\n"
printf " c8l cli install-tools --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_attestation_add_from_yaml_usage() {
printf "c8l cli attestation-add-from-yaml - [aafy] Add to the current atestation based on the yaml file.\n\n"
printf "Alias: aafy\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli attestation-add-from-yaml\n"
printf " c8l cli attestation-add-from-yaml --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_attestation_status_usage() {
printf "c8l cli attestation-status - [as] Get the status of the current attestation.\n\n"
printf "Alias: as\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli attestation-status\n"
printf " c8l cli attestation-status --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_attestation_push_usage() {
printf "c8l cli attestation-push - [ap] Push the current attestation to the Chainloop server.\n\n"
printf "Alias: ap\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli attestation-push\n"
printf " c8l cli attestation-push --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_generate_github_summary_usage() {
printf "c8l cli generate-github-summary - [ggs] Generate a summary of the attestation for GitHub Action.\n\n"
printf "Alias: ggs\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli generate-github-summary\n"
printf " c8l cli generate-github-summary --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_get_attestations_usage() {
printf "c8l cli get-attestations - [ga] Get all attestations for artifact\n\n"
printf "Alias: ga\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli get-attestations [SHA]\n"
printf " c8l cli get-attestations --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf "%s\n" "Arguments:"
printf " %s\n" "SHA"
printf " SHA256 of the artifact.\n"
echo
fi
}
c8l_cli_get_usage() {
printf "c8l cli get - [g] Get artifact from Chainloop\n\n"
printf "Alias: g\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli get [SHA]\n"
printf " c8l cli get --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf "%s\n" "Arguments:"
printf " %s\n" "SHA"
printf " SHA256 of the artifact to get.\n"
echo
printf "%s\n" "Examples:"
printf " cl g 01430ba1c938528a13cea49cd62862c4820e3edc6fc92a9ac5a7f007ea5478ba\n"
echo
fi
}
c8l_cli_workflow_get_usage() {
printf "c8l cli workflow-get - [wg] Get workflow from Chainloop.\n\n"
printf "Alias: wg\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli workflow-get [UUID]\n"
printf " c8l cli workflow-get --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf "%s\n" "Arguments:"
printf " %s\n" "UUID"
printf " UUID of the workflow to get.\n"
echo
printf "%s\n" "Examples:"
printf " cl wg be52251b-0d8a-4719-bc20-16a586555ea4\n"
echo
fi
}
c8l_cli_workflow_list_usage() {
printf "c8l cli workflow-list - [wl] List workflows from Chainloop.\n\n"
printf "Alias: wl\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli workflow-list\n"
printf " c8l cli workflow-list --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
c8l_cli_workflow_run_get_usage() {
printf "c8l cli workflow-run-get - [wrg] Get workflow run from Chainloop.\n\n"
printf "Alias: wrg\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli workflow-run-get [UUID]\n"
printf " c8l cli workflow-run-get --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf "%s\n" "Arguments:"
printf " %s\n" "UUID"
printf " UUID of the workflow run to get.\n"
echo
printf "%s\n" "Examples:"
printf " cl wrg be52251b-0d8a-4719-bc20-16a586555ea4\n"
echo
fi
}
c8l_cli_workflow_run_list_usage() {
printf "c8l cli workflow-run-list - [wrl] List workflow runs from Chainloop.\n\n"
printf "Alias: wrl\n"
echo
printf "%s\n" "Usage:"
printf " c8l cli workflow-run-list\n"
printf " c8l cli workflow-run-list --help | -h\n"
echo
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
fi
}
normalize_input() {
local arg flags passthru
passthru=false
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $passthru == true ]]; then
input+=("$arg")
elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
elif [[ "$arg" == "--" ]]; then
passthru=true
input+=("$arg")
else
input+=("$arg")
fi
shift
done
}
export CHAINLOOP_BIN_PATH="${CHAINLOOP_BIN_PATH:-/usr/local/bin/chainloop_bin}"
is_chainloop_in_path() {
if command -v chainloop &>/dev/null; then
# we are good
return 0
else
log "chainloop is not in PATH, install it."
return 1
fi
}
validate_env() {
is_chainloop_in_path
if [ ! $? ]; then
exit 1
fi
}
t=$(date "+%Y%m%d%H%M%S")
script_dir="$(cd "$(dirname "$0")" && pwd)"
CHAINLOOP_TMP_DIR="${CHAINLOOP_TMP_DIR:-${script_dir}/tmp/chainloop}"
l() {
yellow "$*"
}
log() {
# echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
yellow "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
}
log_error() {
red_bold "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
}
log_header() {
blue_bold "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
}
prepare_tmp_file() {
tmp_dir="${CHAINLOOP_TMP_DIR}"
file_name=$1
mkdir -p "${tmp_dir}"
t="${tmp_dir}/${file_name}"
if [ -f "$t" ]; then
echo "Temporary file file $t already exists"
return 1
fi
echo "$t"
}
# chainloop_bin_cache_in_dir - it takes a path and copy there the CHAINLOOP_BIN_PATH
chainloop_bin_cache_in_dir() {
mkdir -p $1
cp -r $CHAINLOOP_BIN_PATH/* $1
}
# chainloop_recreate_env_from_cache - it takes the path to the file in the format .env_NAME and export env variable NAME=`what is in the file`
# for instance .c8l_cache/.env_chainloop_attestation_id end exports variable CHAINLOOP_ATTESTATION_ID with the value from the file
# or .c8l_cache/.env_chainloop_signing_key_path end exports variable CHAINLOOP_SIGNING_KEY_PATH with the value from the file
# it validates the file exists and is not empty and the file name is in the format .env_NAME
chainloop_recreate_env_from_file() {
path=$1
# if the path is empty or does not exist, return
if [ -z "$path" ] || [ ! -f "$path" ]; then
log_error "chainloop_recreate_env_from_file: path $path does not exist"
return 1
fi
file=$(basename $path)
if [[ $file =~ ^\.env_.*$ ]]; then
export $(echo $file | sed 's/\.env_//')=$(cat $path)
echo export $(echo $file | sed 's/\.env_//')=$(cat $path)
else
log_error "File $file is not in the format .env_NAME"
return 1
fi
}
# chainloop_save_env_to_file - it takes the name of the env variable and saves it to the file in the format .env_NAME
chainloop_save_env_to_file() {
name=$1
dir=$2
if [ -z "${name}" ]; then
log_error "Name is not set"
return 1
fi
# if $dir is empty or directory #$dir does not exist, return
if [ -z "${dir}" ] || [ ! -d "${dir}" ]; then
log_error "Directory $dir does not exist"
return 1
fi
if [ -z "${!name}" ]; then
log_error "Variable $name is not set"
return 1
fi
echo "${!name}" >$dir/.env_${name}
}
# chainloop_save_env_to_cache - it takes the cache directory which is the first parameter and then the list of env variables which are saved to the cache
# it validates directory exists and is a directory
chainloop_save_env_to_cache() {
cache_dir=$1
if [ -z "${cache_dir}" ] || [ ! -d "${cache_dir}" ]; then
log_error "Cache directory $cache_dir does not exist"
return 1
fi
for name in "${@:2}"; do
chainloop_save_env_to_file $name $cache_dir
done
}
# chainloop_restore_env_all_from_cache - it finds all the .env_NAME files in the directory specified as first argument and exports the env variables
# it validates if the directory exists and is a directory
chainloop_restore_env_all_from_cache() {
cache_dir=$1
if [ -z "${cache_dir}" ] || [ ! -d "${cache_dir}" ]; then
log_error "Cache directory $cache_dir does not exist"
return 1
fi
for file in $cache_dir/.env_*; do
chainloop_recreate_env_from_file $file
done
}
# chainloop_restore_all_from_cache - it restores the env variables and the tools from the cache
# it takes the cache directory as the first parameter and validates if it exists and is a directory
chainloop_restore_all_from_cache() {
cache_dir=$1
if [ -z "${cache_dir}" ] || [ ! -d "${cache_dir}" ]; then
log_error "Cache directory $cache_dir does not exist"
return 1
fi
chainloop_restore_env_all_from_cache $cache_dir
chainloop_bin_install $cache_dir/*
}
install_chainloop_labs() {
mkdir -p ${CHAINLOOP_TMP_DIR}
branch=${1:-main}
generic_install c8l https://raw.githubusercontent.com/chainloop-dev/labs/${branch}/tools/c8l
}
# how to use it:
# source <(c8l source)
source_chainloop_labs() {
script_path=$(readlink -f "$0")
echo "export PATH=\"/usr/local/bin:$CHAINLOOP_BIN_PATH:$PATH\""
cat $script_path | sed '$d' | sed '$d'
}
chainloop_bin_install() {
# it takes the list of paths and installs them in the CHAINLOOP_BIN_PATH
# it creates the directory if it does not exist
# it makes the files executable
# make sure to detect the basename of the path and properly make the target file executable
mkdir -p $CHAINLOOP_BIN_PATH
for path in "$@"; do
if [ -f $path ]; then
log "Installing $path"
cp $path $CHAINLOOP_BIN_PATH
chmod +x $CHAINLOOP_BIN_PATH/$(basename $path)
else
log_error "chainloop_bin_install: path $path does not exist"
return 1
fi
done
}
install_chainloop_cli() {
mkdir -p ${CHAINLOOP_TMP_DIR} $CHAINLOOP_BIN_PATH
log "Installing Chainloop CLI"
url_chainloop_cli="https://docs.chainloop.dev/install.sh"
if [ -n "${CHAINLOOP_VERSION}" ]; then
curl -sfL "${url_chainloop_cli}" | bash -s -- --version v${CHAINLOOP_VERSION} --path $CHAINLOOP_BIN_PATH
else
curl -sfL "${url_chainloop_cli}" | bash -s -- --path $CHAINLOOP_BIN_PATH
fi
if [ $? -ne 0 ]; then
log_error "Chainloop installation failed"
return 1
fi
}
# chainloop_install takes the list of tools and installs them with functions below install_${name}
chainloop_install() {
for tool in "$@"; do
if ! "install_${tool}" ; then
return 1
fi
done
}
generic_install() {
file=$1
url=$2
file_path=$CHAINLOOP_BIN_PATH/$file
mkdir -p $CHAINLOOP_BIN_PATH
log "Installing $file"
curl -sfL $url -o $file_path
if [ $? -ne 0 ]; then
log_error "$file installation failed"
return 1
fi
chmod +x $file_path
}
spdx2cyclonedx() {
spdx_file=$1
cyclonedx_file=$2
syft convert "${spdx_file}" -o cyclonedx-json="${cyclonedx_file}"
if [ $? -ne 0 ]; then
log_error "SPDX to CycloneDX conversion failed"
return 1
fi
}
chainloop_attestation_init() {
log "Initializing Chainloop Attestation"
CR_VALUE=""
if [ -n "${CHAINLOOP_CONTRACT_REVISION}" ]; then
CR_VALUE="--contract-revision ${CHAINLOOP_CONTRACT_REVISION}"
fi
WF_NAME_VALUE=""
if [ -n "${CHAINLOOP_WORKFLOW_NAME}" ]; then
WF_NAME_VALUE="--workflow ${CHAINLOOP_WORKFLOW_NAME}"
fi
PROJECT_NAME_VALUE=""
if [ -n "${CHAINLOOP_PROJECT_NAME}" ]; then
PROJECT_NAME_VALUE="--project ${CHAINLOOP_PROJECT_NAME}"
fi
r=$(chainloop attestation init -f --remote-state --output json $CR_VALUE $WF_NAME_VALUE $PROJECT_NAME_VALUE)
if [ $? -ne 0 ]; then
log_error "Chainloop initialization failed: $r"
return 1
fi
export CHAINLOOP_ATTESTATION_ID=$(echo $r | grep attestationID | awk -F\" '{print $4}')
log "Attestation ID: $CHAINLOOP_ATTESTATION_ID"
}
chainloop_attestation_status() {
log "Checking Attestation Status"
if chainloop attestation status --full --remote-state --attestation-id "${CHAINLOOP_ATTESTATION_ID}" &>c8-status.txt; then
log "Attestation Status Process Completed Successfully"
cat c8-status.txt
else
exit_code=$?
log_error "Attestation Status Process Failed"
cat c8-status.txt
return $exit_code
fi
}
chainloop_attestation_push() {
log "Pushing Attestation"
if [ -n "${CHAINLOOP_USE_INSECURE_KEY}" ]; then
log "# USING INSECURE KEY BECAUSE CHAINLOOP_USE_INSECURE_KEY is set"
CHAINLOOP_SIGNING_KEY_PATH="cosign.key"
export CHAINLOOP_SIGNING_PASSWORD=""
export COSIGN_PASSWORD="$CHAINLOOP_SIGNING_PASSWORD"
cosign generate-key-pair
fi
if [ -n "${CHAINLOOP_SIGNING_KEY}" ]; then
log " with CHAINLOOP_SIGNING_KEY"
tmp_key="${CHAINLOOP_TMP_DIR}/key"
mkdir -p "${CHAINLOOP_TMP_DIR}"
echo "${CHAINLOOP_SIGNING_KEY}" > "$tmp_key"
fi
if [ -n "${CHAINLOOP_SIGNING_KEY_PATH}" ]; then
log " with CHAINLOOP_SIGNING_KEY_PATH"
tmp_key="${CHAINLOOP_SIGNING_KEY_PATH}"
fi
tmp_key_value=""
if [ -n "$tmp_key" ]; then
tmp_key_value="--key $tmp_key"
fi
# chainloop attestation push --key env://CHAINLOOP_SIGNING_KEY
if chainloop attestation push "$tmp_key_value" --output json --remote-state --attestation-id "${CHAINLOOP_ATTESTATION_ID}" > c8-push.txt; then
log "Attestation Process Completed Successfully"
cat c8-push.txt
rm -f "$tmp_key"
else
exit_code=$?
log_error "Attestation Process Failed"
cat c8-push.txt
rm -f "$tmp_key"
return $exit_code
fi
}
chainloop_summary() {
tmpfile=$(prepare_tmp_file report.txt)
if [ $? -ne 0 ]; then
log $tmpfile
return 1
fi
echo -e "## Great job!\n\nYou are making SecOps and Compliance teams really happy. Keep up the good work!\n" >> $tmpfile
digest=""
if [ -f c8-push.txt ]; then
digest=$(cat c8-push.txt | jq -r '.digest')
if [ $? -ne 0 ]; then
log_error "Failed to get digest from c8-push.txt"
return 1
fi
echo "**[Chainloop Trust Report]( https://app.chainloop.dev/attestation/${digest} )**" >> "$tmpfile"
fi
if [ -f c8-status.txt ] ; then
echo "\`\`\`" >> "$tmpfile"
cat c8-status.txt >> "$tmpfile"
echo "\`\`\`" >> "$tmpfile"
fi
cat "$tmpfile"
rm "$tmpfile"
}
chainloop_summary_on_failure() {
log "Generating Summary on Failure"
tmpfile=$(prepare_tmp_file report_on_failure.txt)
echo -e "## Chainloop Attestation Failed\nWe were unable to complete the Chainloop attestation process due to unmet SecOps and Compliance requirements:" >>$tmpfile
if [ -f c8-push.txt ]; then
echo -e "\n> [!WARNING]" >>$tmpfile
cat c8-push.txt | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" | sed 's/^/> /' >>$tmpfile
echo -e "\n" >>$tmpfile
fi
if [ -f c8-status.txt ]; then
echo "\`\`\`" >>$tmpfile
cat c8-status.txt | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" >>$tmpfile
echo "\`\`\`" >>$tmpfile
fi
cat $tmpfile
rm $tmpfile
}
chainloop_generate_github_summary() {
chainloop_summary >>$GITHUB_STEP_SUMMARY
}
chainloop_generate_github_summary_on_failure() {
chainloop_summary_on_failure >>$GITHUB_STEP_SUMMARY
}
chainloop_collect_logs_for_github_jobs() {
log "Collecting logs for GitHub Jobs"
# GH_TOKEN or GITHUB_TOKEN is required for this to work
# https://docs.github.com/en/rest/actions/workflow-jobs?apiVersion=2022-11-28#download-job-logs-for-a-workflow-run
mkdir -p metadata/gh_logs
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs >metadata/gh_logs/jobs.json
for j in $(cat metadata/gh_logs/jobs.json | jq '.jobs[].id'); do
gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${GITHUB_REPOSITORY}/actions/jobs/${j}/logs >metadata/gh_logs/${j}.log
done
}
chainloop_attestation_add_from_yaml() {
export ATTESTATION_NAME=$1
if [ -z "${ATTESTATION_NAME}" ]; then
export ATTESTATION_NAME="attestation"
fi
log "Adding Metadata files based on .chainloop.yml"
script=$(cat .chainloop.yml | yq eval '.[strenv(ATTESTATION_NAME)][] | "chainloop attestation add --name " + .name + " --value " + .path + " --remote-state --attestation-id ${CHAINLOOP_ATTESTATION_ID} 2>&1; "')
log $script
eval $script
}
print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
yellow() { print_in_color "\e[33m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
magenta() { print_in_color "\e[35m" "$*"; }
cyan() { print_in_color "\e[36m" "$*"; }
bold() { print_in_color "\e[1m" "$*"; }
underlined() { print_in_color "\e[4m" "$*"; }
red_bold() { print_in_color "\e[1;31m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
blue_bold() { print_in_color "\e[1;34m" "$*"; }
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
red_underlined() { print_in_color "\e[4;31m" "$*"; }
green_underlined() { print_in_color "\e[4;32m" "$*"; }
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }
install_chainloop_tools() {
log_header "Installing Chainloop Tools"
export PATH=$CHAINLOOP_BIN_PATH:$PATH
if [ -f $CHAINLOOP_BIN_PATH/chainloop ]; then
log "Skipping... Chainloop Tools already installed"
return 0
fi
install_chainloop_cli_and_cosign
install_syft
install_semgrep
install_oras
install_jq
install_yq
install_parlay
}
install_cosign() {
generic_install cosign https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64
}
install_chainloop_cli_and_cosign() {
install_cosign
install_chainloop_cli
}
install_syft() {
log "Installing Syft"
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b $CHAINLOOP_BIN_PATH
if [ $? -ne 0 ]; then
log_error "Syft installation failed"
return 1
fi
}
install_semgrep() {
log "Installing Semgrep"
python3 -m pip install semgrep -q >$CHAINLOOP_TMP_DIR/install_semgrep.log
if [ $? -ne 0 ]; then
log_error "Semgrep installation failed"
return 1
fi
}
install_conftest() {
LATEST_VERSION=$(wget -O - "https://api.github.com/repos/open-policy-agent/conftest/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | cut -c 2-)
wget -q "https://github.com/open-policy-agent/conftest/releases/download/v${LATEST_VERSION}/conftest_${LATEST_VERSION}_Linux_x86_64.tar.gz" >$CHAINLOOP_TMP_DIR/install_wget.log
tar xzf conftest_${LATEST_VERSION}_Linux_x86_64.tar.gz
$SUDO_CMD mv conftest $CHAINLOOP_BIN_PATH
}
install_oras() {
log "Installing Oras"
VERSION="1.0.0"
curl -sLO "https://github.com/oras-project/oras/releases/download/v${VERSION}/oras_${VERSION}_linux_amd64.tar.gz"
mkdir -p oras-install/
tar -zxf oras_${VERSION}_*.tar.gz -C oras-install/
$SUDO_CMD mv oras-install/oras $CHAINLOOP_BIN_PATH
rm -rf oras_${VERSION}_*.tar.gz oras-install/
oras version
if [ $? -ne 0 ]; then
log_error "Oras installation failed"
return 1
fi
}
install_jq() {
generic_install jq https://github.com/jqlang/jq/releases/latest/download/jq-linux-amd64
}
install_ruby_restclient() {
log "Installing Ruby rest-client"
$SUDO_CMD gem install rest-client >$CHAINLOOP_TMP_DIR/install_ruby_restclient.log
if [ $? -ne 0 ]; then
log_error "rest-client installation failed"
return 1
fi
}
install_yq() {
generic_install yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
}
install_parlay() {
log "Installing Parlay"
wget -q https://github.com/snyk/parlay/releases/latest/download/parlay_Linux_x86_64.tar.gz >$CHAINLOOP_TMP_DIR/install_parlay.log
tar -xvf parlay_Linux_x86_64.tar.gz
$SUDO_CMD mv parlay $CHAINLOOP_BIN_PATH
rm parlay_Linux_x86_64.tar.gz
}
c8l_help_command() {
command="${args[command]}"
long_usage=yes
if [[ -z "$command" ]]; then
# No command argument, show the global help
help_function=c8_usage
else
# Show the help for the requested command
help_function="c8_${command}_usage"
fi
# Call the help function if it exists
if [[ $(type -t "$help_function") ]]; then
"$help_function"
else
echo "No help available for this command"
exit 1
fi
}
c8l_inspect_command() {
validate_env
chainloop version
l "\n\n ORG"
chainloop org describe -o json
l "\n\n CAS Backends"
chainloop cas-backend list -o json
}
c8l_source_command() {
source_chainloop_labs
}
c8l_cmd_command() {
eval "${args['command']}"
}
c8l_cli_install_tools_command() {
validate_env
install_chainloop_tools
}
c8l_cli_attestation_add_from_yaml_command() {
validate_env
chainloop_attestation_add_from_yaml
}
c8l_cli_attestation_status_command() {
validate_env
chainloop_attestation_status