Skip to content

Commit

Permalink
test(test-data): add layers information to the image metadata json (#347
Browse files Browse the repository at this point in the history
)

* test(test-data): add layers information to the image metadata json

Signed-off-by: Andrei Aaron <[email protected]>

* fix(tests): fix username userd as password, fix prerequisite validation

Signed-off-by: Andrei Aaron <[email protected]>

* fix(tests): auto-confirm cosign upload to private registry

Signed-off-by: Andrei Aaron <[email protected]>

---------

Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron authored Apr 27, 2023
1 parent 415973e commit c09a12f
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions tests/scripts/pull_update_push_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cosign_password=""
metafile=""
multiarch=""
username=""
username=""
password=""
debug=0
data_dir=$(pwd)

Expand Down Expand Up @@ -110,28 +110,33 @@ cosign_key_path=${data_dir}/cosign.key
function verify_prerequisites {
mkdir -p ${data_dir}

if [ ! command -v regctl ] &>/dev/null; then
echo "you need to install regctl as a prerequisite" >&3
return 1
command -v regctl
if [ $? -ne 0 ]; then
echo "you need to install regctl as a prerequisite"
exit 1
fi

if [ ! command -v skopeo ] &>/dev/null; then
echo "you need to install skopeo as a prerequisite" >&3
return 1
command -v skopeo
if [ $? -ne 0 ]; then
echo "you need to install skopeo as a prerequisite"
exit 1
fi

if [ ! command -v cosign ] &>/dev/null; then
echo "you need to install cosign as a prerequisite" >&3
command -v cosign
if [ $? -ne 0 ]; then
echo "you need to install cosign as a prerequisite"
return 1
fi

if [ ! command -v trivy ] &>/dev/null; then
echo "you need to install trivy as a prerequisite" >&3
command -v trivy
if [ $? -ne 0 ]; then
echo "you need to install trivy as a prerequisite"
return 1
fi

if [ ! command -v jq ] &>/dev/null; then
echo "you need to install jq as a prerequisite" >&3
command -v jq
if [ $? -ne 0 ]; then
echo "you need to install jq as a prerequisite"
return 1
fi

Expand Down Expand Up @@ -197,7 +202,7 @@ regctl image mod --replace --annotation org.opencontainers.image.documentation="

credentials_args=""
if [ ! -z "${username}" ]; then
credentials_args="--dest-creds ${username}:${username}"
credentials_args="--dest-creds ${username}:${password}"
fi

# Upload image to target registry
Expand All @@ -224,8 +229,27 @@ else
echo '{"trivy":[]}' > ${trivy_out_file}
fi

layers_file=manifests-${image}-${tag}.json
rm -f ${layers_file}

if [ -z "${multiarch}" ]; then
regctl manifest --format raw-body get ${local_image_ref_regtl} | jq '{ manifests: { default: { layers: [ .layers[].digest ] } } }' > ${layers_file}
else
manifests=$(regctl manifest --format raw-body get ${local_image_ref_regtl} | jq '[ .manifests[] | { "digest":.digest, "platform":(.platform | [ .os, .architecture, .variant ] | map(select(.!=null)) | join("/") )} ] ')

echo $manifests | jq -c '.[]' | while read i; do
digest=$(echo $i | jq -r '.digest')
platform=$(echo $i | jq -r '.platform')
regctl manifest --format raw-body get ocidir://${images_dir}@${digest} | jq --arg platform "${platform}" '{ manifests: { ($platform): { layers: [ .layers[].digest ] } } }' >> layers-${image}-${tag}-${digest//:/_}.json
done

jq -n '{ manifests: [ inputs.manifests ] | add }' layers-${image}-${tag}*.json > ${layers_file}
rm -f layers-${image}-${tag}*.json
fi


# Sign new updated image
COSIGN_PASSWORD=${cosign_password} cosign sign ${remote_dest_image_ref} --key ${cosign_key_path} --allow-insecure-registry
COSIGN_PASSWORD=${cosign_password} cosign sign ${remote_dest_image_ref} --key ${cosign_key_path} --allow-insecure-registry --yes
if [ $? -ne 0 ]; then
exit 1
fi
Expand All @@ -242,5 +266,5 @@ jq -n \
--arg org.opencontainers.image.documentation "${description}" \
'$ARGS.named' > ${details_file}

jq -c -s add ${details_file} ${trivy_out_file} > ${metafile}
rm ${details_file} ${trivy_out_file}
jq -c -s add ${details_file} ${trivy_out_file} ${layers_file} > ${metafile}
rm ${details_file} ${trivy_out_file} ${layers_file}

0 comments on commit c09a12f

Please sign in to comment.