| 
 | 1 | +#!/bin/bash  | 
 | 2 | +# Retrieves image configuration from public images in DockerHub  | 
 | 3 | +# Functions from https://gist.github.com/cirocosta/17ea17be7ac11594cb0f290b0a3ac0d1  | 
 | 4 | +# Optimised for our use case  | 
 | 5 | + | 
 | 6 | +get_image_label() {  | 
 | 7 | +  local label=$1  | 
 | 8 | +  local image=$2  | 
 | 9 | +  local tag=$3  | 
 | 10 | +  local token  | 
 | 11 | +  token=$(_get_token "$image")  | 
 | 12 | +  local digest  | 
 | 13 | +  digest=$(_get_digest "$image" "$tag" "$token")  | 
 | 14 | +  local retval="null"  | 
 | 15 | +  if [ "$digest" != "null" ]; then  | 
 | 16 | +    retval=$(_get_image_configuration "$image" "$token" "$digest" "$label")  | 
 | 17 | +  fi  | 
 | 18 | +  echo "$retval"  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +get_image_layers() {  | 
 | 22 | +  local image=$1  | 
 | 23 | +  local tag=$2  | 
 | 24 | +  local token  | 
 | 25 | +  token=$(_get_token "$image")  | 
 | 26 | +  _get_layers "$image" "$tag" "$token"  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +get_image_last_layer() {  | 
 | 30 | +  local image=$1  | 
 | 31 | +  local tag=$2  | 
 | 32 | +  local token  | 
 | 33 | +  token=$(_get_token "$image")  | 
 | 34 | +  local layers  | 
 | 35 | +  mapfile -t layers < <(_get_layers "$image" "$tag" "$token")  | 
 | 36 | +  echo "${layers[-1]}"  | 
 | 37 | +}  | 
 | 38 | + | 
 | 39 | +_get_image_configuration() {  | 
 | 40 | +  local image=$1  | 
 | 41 | +  local token=$2  | 
 | 42 | +  local digest=$3  | 
 | 43 | +  local label=$4  | 
 | 44 | +  curl \  | 
 | 45 | +    --silent \  | 
 | 46 | +    --location \  | 
 | 47 | +    --header "Authorization: Bearer $token" \  | 
 | 48 | +    "https://registry-1.docker.io/v2/$image/blobs/$digest" \  | 
 | 49 | +    | jq -r ".config.Labels.\"$label\""  | 
 | 50 | +}  | 
 | 51 | + | 
 | 52 | +_get_token() {  | 
 | 53 | +  local image=$1  | 
 | 54 | +  curl \  | 
 | 55 | +    --silent \  | 
 | 56 | +    "https://auth.docker.io/token?scope=repository:$image:pull&service=registry.docker.io" \  | 
 | 57 | +    | jq -r '.token'  | 
 | 58 | +}  | 
 | 59 | + | 
 | 60 | +_get_digest() {  | 
 | 61 | +  local image=$1  | 
 | 62 | +  local tag=$2  | 
 | 63 | +  local token=$3  | 
 | 64 | +  curl \  | 
 | 65 | +    --silent \  | 
 | 66 | +    --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \  | 
 | 67 | +    --header "Authorization: Bearer $token" \  | 
 | 68 | +    "https://registry-1.docker.io/v2/$image/manifests/$tag" \  | 
 | 69 | +    | jq -r '.config.digest'  | 
 | 70 | +}  | 
 | 71 | + | 
 | 72 | +_get_layers() {  | 
 | 73 | +  local image=$1  | 
 | 74 | +  local tag=$2  | 
 | 75 | +  local token=$3  | 
 | 76 | +  curl \  | 
 | 77 | +    --silent \  | 
 | 78 | +    --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \  | 
 | 79 | +    --header "Authorization: Bearer $token" \  | 
 | 80 | +    "https://registry-1.docker.io/v2/$image/manifests/$tag" \  | 
 | 81 | +    | jq -r '.layers[].digest'  | 
 | 82 | +}  | 
0 commit comments